/*** 本文件由CSMIS2.0-ModelCreater(UTF-8)工具辅助生成，版本1.0 ***/

package com.ce.sys.log.action;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.DateUtil;
import com.ce.framework.util.StringUtil;
import com.ce.sys.log.view.SysUserOperateLogView;
import com.ce.sys.login.model.User;
import com.ce.util.DbUtil;

/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2010-11-25
* 创建时间：09:27:44
* 功能描述： [SYS_T_USER_OPERATE_LOG]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class SysUserOperateLogAction extends DataMaintainAction 
{
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if("static".equals(this.getParameter("type"))){
			return this.staticGraph();
		}
		return super.execute();
	}

	@SuppressWarnings("unchecked")
	public String staticGraph()throws AppException{
		String nowDate = DateUtil.format(new Date());
		String currMonth = nowDate.split("-")[1];
        String currYear = nowDate.split("-")[0];
        String menuId = "0";
        if(StringUtil.isNotBlank(this.getParameter("menuId")))
        	menuId = this.getParameter("menuId");
        if(StringUtil.isNotBlank(this.getParameter("year")))
        	currYear = this.getParameter("year");
        if(StringUtil.isNotBlank(this.getParameter("month")))
        	currMonth = this.getParameter("month");
        StringBuffer sql = new StringBuffer("select distinct(t1.vc_funname)as funname from sys_t_user_operate_log_"+currYear+"_"+currMonth+" t1 ");
        sql.append("  where t1.vc_funname in (select t.vc_name from sys_t_menu t ");
        //是超级管理员时加载所有菜单，否则只加载系统管理员菜单
		if(!User.getUser().isSuperUser())
			sql.append(" where t.vc_id in (select t2.vc_menu$id from sys_t_role$permiss t2 where t2.vc_role$id ='admin')");
        sql.append(" start with t.vc_par$id = '"+menuId+"' connect by PRIOR t.vc_id = t.vc_par$id)");
		List<SysUserOperateLogView> infoList = (List<SysUserOperateLogView>) DbUtil.executeSqlQuery(sql.toString(), new ISQLMapper(){
			public Object fromSQLQuery(ResultSet rs, int num) {
				// TODO Auto-generated method stub
				SysUserOperateLogView log = new SysUserOperateLogView();
				try {
					log.setVcFunname(rs.getString("funname"));
				} catch (SQLException e) {
				   e.printStackTrace();
				   Logger.getLogger(this.getClass()).error("获取日志信息失败！");
				}
				return log;
			}
		});
		Iterator<SysUserOperateLogView> it = infoList.iterator();
		String menuName = "";
		String menuCnt = "";
		while(it.hasNext()){
			SysUserOperateLogView log = it.next();
			int cnt = DbUtil.getFirstIntSqlQuery("select count(*) from sys_t_user_operate_log_"+currYear+"_"+currMonth+" t where t.vc_funname = '"+log.getVcFunname()+"'");
            menuName = menuName + "'"+log.getVcFunname()+"',";
            menuCnt = menuCnt + cnt+ ",";
		}
		if(StringUtil.isNotBlank(menuName))
			menuName = menuName.substring(0,menuName.length()-1);
		if(StringUtil.isNotBlank(menuCnt))
			menuCnt = menuCnt.substring(0,menuCnt.length()-1);
		setAttribute("menuName", menuName);
		setAttribute("menuCnt", menuCnt);
		setAttribute("month", currMonth);
		setAttribute("year", currYear);
		setAttribute("menuId", menuId);
		return "viewStaticPage";


	}
}

